package cn.Product.controller;

import cn.Product.service.ProductService;
import cn.User.service.UserService;
import cn.YstHome.service.YstHomeService;
import cn.controller.AopController;
import cn.util.aop.Authority;
import cn.util.json.JacksonUtil;
import com.alibaba.fastjson.JSON;
import common.constant.ServiceResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品接口类
 */
@Controller
@RequestMapping("product")
public class ProductController {
    private Logger logger=LoggerFactory.getLogger(AopController.class);

    /**
     * 首页接口
     */
    @Autowired
    YstHomeService ystHomeService;


    @Autowired
     ProductService productService;

    /**
     * 用户接口
     */
    @Autowired
    UserService userService;

    /**
     * 获取列表商品
     * @param json
     * @return
     */
     @ResponseBody
     @RequestMapping("/list")
     public Object getProductList(@RequestBody String json){
         logger.info("访问地址===product/list;访问参数==="+json);
         Map<String,Object> map=new HashMap<>();

         return JSON.toJSONString(productService.getProductList(map));
      }
    /**
     * 获取主页随机商品
     */
    @ResponseBody
    @RequestMapping("/random")
    public Object getProductRandom(@RequestBody String json){
        logger.info("访问地址===product/random;访问参数==="+json);
        Map<String,Object> map=new HashMap<>();

        return JSON.toJSONString(productService.getProductRandom(map));
    }
    /**
     * 获取商品类别
     * @param json
     * @return
     */
    @ResponseBody
    @RequestMapping("/type")
    public Object getProductType(@RequestBody String json){
        logger.info("访问地址===product/type;访问参数==="+json);
        Map<String,Object> map=new HashMap<>();

        return JSON.toJSONString(productService.getProductType(map));
    }
    /**
     * 进入商品
     */
    @RequestMapping("{SPBH}.html")
    @Authority()
    public Object Index(@RequestBody String json, @PathVariable String SPBH, Model model) {
        logger.info("访问地址===product/"+SPBH+".html;访问参数==="+json);
        //传过来的数据
        Map<String,Object> map= null;
        //返回的数据
        Map<String,Object> result=new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            map.put("SPBH",SPBH);
            result=productService.getProductInfo(map);
            //获取导航信息
            result.put("YstInfoNav",ystHomeService.getYstInfoNav(map));
            //获取登录用户信息
            result.put("YstUserInfo",userService.getuserinfo(map));
            //返回模板
            map.put("path","Product/product");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ServiceResult.createResult(model,map,result);

    }
    /**
     * 根据类型获取商品
     */
    @RequestMapping("{JiBie}/{LXMC}")
    @Authority()
    public Object getTypeProductList(@RequestBody String json, @PathVariable String JiBie, @PathVariable String LXMC,Model model)   {
        logger.info("访问地址===product/"+JiBie+"/"+LXMC+";访问参数==="+json);
        //传过来的数据
        Map<String,Object> map= null;
        //返回的数据
        Map<String,Object>  result=new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            map.put(JiBie,LXMC);
            //根据类型获取商品
            result.put("TypeProductList",productService.getTypeProductList(map));
            //商品类型
            result.put("ProductType",productService.getProductType(map));
            //获取导航信息
            result.put("YstInfoNav",ystHomeService.getYstInfoNav(map));
            //获取登录用户信息
            result.put("YstUserInfo",userService.getuserinfo(map));
            //回显数据
            result.put("JiBie",JiBie);
            result.put("LXMC",LXMC);
            //返回模板
            map.put("path","Product/typeProduct");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ServiceResult.createResult(model,map,result);

    }

}
